package com.musifind.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import com.musifind.activities.R;
import com.musifind.activities.SearchActivity;
import com.musifind.models.SearchItems;

public class SearchAdapter extends BaseAdapter implements Filterable {

	private SearchActivity activity;
	private ItemFilter searchFilter;
	private Typeface typeface;
	private ArrayList<SearchItems> searchList;
	private ArrayList<SearchItems> filteredList;

	public SearchAdapter(SearchActivity activity, ArrayList<SearchItems> friendList) {
		this.activity = activity;
		this.searchList = friendList;
		this.filteredList = friendList;
		typeface = Typeface.createFromAsset(activity.getAssets(), "Opificio.ttf");

		getFilter();
	}

	@Override
	public int getCount() {
		return filteredList.size();
	}

	@Override
	public Object getItem(int i) {
		return filteredList.get(i);
	}

	@Override
	public long getItemId(int i) {
		return i;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		// A ViewHolder keeps references to children views to avoid unnecessary calls
		// to findViewById() on each row.
		final ViewHolder holder;
		final SearchItems user = (SearchItems) getItem(position);

		if (view == null) {
			LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = layoutInflater.inflate(R.layout.search_list_items_row_tracks, parent, false);
			holder = new ViewHolder();
			holder.coverImage = (ImageView) view.findViewById(R.id.imgAlbumCover);
			holder.trackName = (TextView) view.findViewById(R.id.txtTrackName);
			holder.artistName = (TextView) view.findViewById(R.id.txtArtistName);
			holder.albumName = (TextView) view.findViewById(R.id.txtAlbumName);
			holder.albumYear = (TextView) view.findViewById(R.id.txtAlbumYear);
			holder.trackName.setTypeface(typeface, Typeface.BOLD);
			// holder.trackName.setTextColor(activity.getResources().getColor(R.color.white));
			holder.artistName.setTypeface(typeface, Typeface.NORMAL);

			view.setTag(holder);
		} else {
			// get view holder back
			holder = (ViewHolder) view.getTag();
		}

		// bind text with view holder content view for efficient use
		holder.trackName.setText("Comfortably Numb");
		holder.artistName.setText("Pink Floyd");
		holder.albumName.setText("The Wall");
		holder.albumYear.setText("(1982)");
		// view.setBackgroundResource(R.drawable.search_list_selector);

		return view;
	}

	/**
	 * Get custom filter
	 * 
	 * @return filter
	 */
	@Override
	public Filter getFilter() {
		if (searchFilter == null) {
			searchFilter = new ItemFilter();
		}

		return searchFilter;
	}

	static class ViewHolder {
		ImageView coverImage;
		TextView trackName;
		TextView artistName;
		TextView albumName;
		TextView albumYear;
	}

	private class ItemFilter extends Filter {

		@Override
		protected FilterResults performFiltering(CharSequence constraint) {
			FilterResults filterResults = new FilterResults();
			if (constraint != null && constraint.length() > 0) {
				ArrayList<SearchItems> tempList = new ArrayList<SearchItems>();

				// search content in friend list
				for (SearchItems searchItems : searchList) {
					if (searchItems.getTrackName().toLowerCase().contains(constraint.toString().toLowerCase())) {
						tempList.add(searchItems);
					}
				}

				filterResults.count = tempList.size();
				filterResults.values = tempList;
			} else {
				filterResults.count = searchList.size();
				filterResults.values = searchList;
			}

			return filterResults;
		}

		@SuppressWarnings("unchecked")
		@Override
		protected void publishResults(CharSequence constraint, FilterResults results) {
			filteredList = (ArrayList<SearchItems>) results.values;
			notifyDataSetChanged();
		}
	}

}